<template>
	<view>
		<block>
			<uni-nav-bar status-bar="true" shadow="true" title="我的"></uni-nav-bar>
		</block>
		<view style="position: relative;">
			<view @tap="loginWeinxin" v-if="!weixinLogined" style="height:50px;">
				<image :src="avatarurl" mode="widthFix" style=" border-radius: 50px; float: left;   width: 50px; height: 100%; margin-left: 10px; "></image>
				<view style="height: 50px;  display: flex;  align-items:center;">
					<view style="color: #007AFF; font-size: larger; padding-left: 10px;">登录/注册</view>
				</view>
			</view>
			<view @tap="loginWeinxin" v-else style="height:50px;">
				<image :src="avatarurl" mode="widthFix" style="border-radius: 50px; float: left;   width: 50px; height: 100%; margin-left: 10px;"></image>
				<view style="height: 50px;  display: flex;  align-items:center;">
					<view>
						<view style="font-size: larger; padding-left: 10px;">{{nickname}}</view>
						<view>
							<view style="font-size: larger; padding-left: 10px; display: inline-block;">{{telephone}}</view>
							<text @tap="changeLogin" style=" color: #007AFF; display: inline-block; margin-left:15px;">修改</text>
						</view>
						
					</view>
				</view>
			</view>
			<view class="under-line"></view>
			<uni-list>
				<block v-for="(item,index) in myItems" :key="index">
					<uni-list-item  @tap="goMyTopicList(index)" :title=item />
				</block>
			</uni-list>
			
		</view>
		<button  style="position: absolute; bottom: 10%; width: 100%;" v-if="weixinLogined"  @tap="logOut">退出登录</button>
	</view>
</template>

<script>
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import config from "@/common/config.js"
	import uniList from '@/components/uni-list/uni-list.vue'
	import uniListItem from '@/components/uni-list-item/uni-list-item.vue'
	export default {
		components: {
			uniNavBar,
			uniList,
			uniListItem
		},
		data() {
			return {
				weixinUserInfo: "",
				weixinLogined: false,
				weixinsession: '',
				myItems: ['我的记录', '我的收藏', '我的消息'],
				telephone: '',
				nickname: '',

				avatarurl: 'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJFkZ1CdGLb8SAGPhibuKsfBjOtabSfHbX3RF27Y0azoCRicIlUF8qdmt7uicIruaKsc8DOk6t55jJew/132'

			}
		},
		methods: {
			onShow() {
				//检查weixin的session是否过期
				uni.checkSession({
					success: (e) => {
						this.weixinsession = uni.getStorageSync(config.sessionkey);
						if (this.weixinsession.length > 0) {
							console.log("weiguoqi==" + this.weixinsession);
							var token = uni.getStorageSync(config.token);
							this.telephone = uni.getStorageSync('telephone');
							this.nickname = uni.getStorageSync('nickname');
							var avatarurl = uni.getStorageSync('avatarurl');
							this.avatarurl = avatarurl.length > 0 ? avatarurl : this.avatarurl;
							this.weixinLogined = this.telephone.length > 0 && this.nickname.length > 0 && token.length > 0;
						} else {
							this.weixinLogined = false;
							console.log("yiguoqio" + this.weixinsession)
						}

					},
					fail: () => {
						this.weixinLogined = false;
						console.log("yijinggoqi")
					}
				});
				// uni.setStorageSync('telephone', res.data.thirdBind.telephone);
				// uni.setStorageSync('nickname', res.data.thirdBind.nickname);
				// uni.setStorageSync('avatarurl', res.data.thirdBind.avatarurl);


			},
			//修改时，需要先去判断是否先去授权页
			changeLogin(){
				uni.checkSession({
					success: (e) => {
						this.weixinsession = uni.getStorageSync(config.sessionkey);
						if (this.weixinsession.length > 0) {
							uni.navigateTo({
								url: '../loginbytelephone/loginbytelephone'
							})
						} else {
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst?goto=loginbytelephone'
							})
						}
				
					},
					fail: () => {
						console.log("yijinggoqdi")
						uni.navigateTo({
							url: '../authorityRequst/authorityRequst?goto=loginbytelephone'
						})
					}
				});
				
			},
			//第三方微信登录
			loginWeinxin() {
				// this.getWinxinInfo();// 2月13号将其注销，相关业务写在了权限页面
				if (!this.weixinLogined) {
					// uni.navigateTo({
					// 	url: '../authorityRequst/authorityRequst'
					// });
					
					uni.checkSession({
						success: (e) => {
							this.weixinsession = uni.getStorageSync(config.sessionkey);
							if (this.weixinsession.length > 0) {
								uni.navigateTo({
									url: '../loginnew/loginnew'
								})
							} else {
								uni.navigateTo({
									url: '../authorityRequst/authorityRequst'
								})
							}
					
						},
						fail: () => {
							console.log("yijinggoqdi")
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst'
							})
						}
					});
				}
			},
			goMyTopicList(index) {
				var url = '../mytopiclist/mytopiclist?id=' + index
				if (index == 2) {
					url = '../mynews/mynews';
				}
				uni.navigateTo({
					url
				})
			},
			// goLoginPage() {
			// 	uni.navigateTo({
			// 		url: '../loginnew/loginnew?from=my',

			// 	})
			// },
			logOut() {

				uni.clearStorageSync();
				this.weixinLogined = false;
			}

		}
	}
</script>

<style>
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.under-line {
		width: auto;
		height: 20upx;
		background-color: #E5E5E5;
	}
	
</style>
